package com.hsytech.dijiebao.person;

import java.util.HashMap;
import java.util.List;

import com.hsytech.dijiebao.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SeeCityAdapter extends BaseAdapter {
    private Context context;
    private List<HashMap<String, Object>> listData;
    private LayoutInflater inflater;
    private View myitemview;
    private int po;
    private boolean isAllSelected = false;
    private boolean isAllDeSelected = false;

    public SeeCityAdapter(Context context, List<HashMap<String, Object>> listdata) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.listData = listdata;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        HashMap<String, Object> data = listData.get(position);
        int type = (Integer) data.get("type");
        return type;
    }

    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return 2;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public HashMap<String, Object> getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View itemView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (itemView == null) {
            viewHolder = new ViewHolder();
            itemView = inflater.inflate(R.layout.detail_list_see_city, null);
            viewHolder.layout_item = (RelativeLayout) itemView.findViewById(R.id.layout_item);
            viewHolder.itemText = (TextView) itemView.findViewById(R.id.city);
            viewHolder.imageCheck = (ImageView) itemView.findViewById(R.id.check_box);
            itemView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) itemView.getTag();
        }
        HashMap<String, Object> map = getItem(position);
        if (map != null) {
            int viewType = getItemViewType(position);
            if (viewType == 0) {
                viewHolder.itemText.setText((String) map.get("city1"));
                viewHolder.layout_item.setBackgroundColor(context.getResources().getColor(R.color.gary02_first));
                viewHolder.imageCheck.setVisibility(View.GONE);
            } else {

                boolean isChecked = (Boolean) map.get("checked");
                viewHolder.itemText.setText((String) map.get("city"));
                viewHolder.imageCheck.setImageResource(isChecked ? R.mipmap.blue_yes : R.mipmap.gary_no);

            }
        }

        return itemView;
    }


    public void setSelected(boolean isSelect) {

        for (int i = 0; i < listData.size(); i++) {
            HashMap<String, Object> map = getItem(i);
            if (map != null) {
                int viewType = getItemViewType(i);
                if (viewType == 0) {
                    map.put("checked", false);
                } else {
                    if (isSelect) {
                        map.put("checked", true);

                    } else {
                        map.put("checked", false);
                    }


                }
            }

        }
        notifyDataSetChanged();
    }


    private static class ViewHolder {
        TextView itemText;
        ImageView imageCheck;
        RelativeLayout layout_item;
    }
}
